package com.org.khtn.gui;

import java.util.Map;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import com.org.khtn.data.Manager;

public class MyExplorer extends JTree {
	DefaultTreeModel model;

	public MyExplorer(Map database) {
		DefaultMutableTreeNode top = new DefaultMutableTreeNode("Server");
		createNodes(top, database);
		model = (DefaultTreeModel) getModel();
		model.setRoot(top);
		getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
	}

	public void createNodes(DefaultMutableTreeNode top,
			Map<String, Vector> database) {
		DefaultMutableTreeNode databases = new DefaultMutableTreeNode(
				"Databases");
		for (Map.Entry<String, Vector> e : database.entrySet()) {
			DefaultMutableTreeNode userDatabase = new DefaultMutableTreeNode(
					e.getKey());
			DefaultMutableTreeNode tables = new DefaultMutableTreeNode("Tables");
			Vector table = (Vector) e.getValue();
			for (int i = 0; i < table.size(); i++) {
				String userTable = (String) table.elementAt(i);
				tables.add(new DefaultMutableTreeNode(userTable));
			}
			userDatabase.add(tables);
			databases.add(userDatabase);
		}
		top.add(databases);
	}

	public void deleteNode(DefaultMutableTreeNode node) {
		model.removeNodeFromParent(node);
	}

	public void insertNode(DefaultMutableTreeNode parent, String nodeName) {
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(nodeName);
		if (parent.toString().compareToIgnoreCase("Databases") == 0) {
			DefaultMutableTreeNode tables = new DefaultMutableTreeNode("Tables");
			model.insertNodeInto(tables, node, node.getChildCount());
		}
		model.insertNodeInto(node, parent, parent.getChildCount());
	}
}
